package org.brownflat.dbinit.upgrade;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * <p>Database Init &copy Rupert Jones 2013</p>
 *
 * @author rup
 */
public abstract class AbstractUpgradeSource implements UpgradeSource {
    @Override
    public List<Upgrade> getUpgradesFrom(int sequence) {
        List<Upgrade> upgradesFrom = new ArrayList<Upgrade>();
        List<Upgrade> upgrades = getUpgrades();

        for(Upgrade thisUpgrade : upgrades) {
            if(thisUpgrade.getSequence() > sequence) {
                upgradesFrom.add(thisUpgrade);
            }
        }
        return upgradesFrom;
    }

    protected void sort(List<Upgrade> upgrades) {
         Collections.sort(upgrades, new Comparator<Upgrade>() {
             @Override
             public int compare(Upgrade o1, Upgrade o2) {
                 if (o1.getSequence() > o2.getSequence()) {
                     return 1;
                 } else if (o1.getSequence() < o2.getSequence()) {
                     return -1;
                 } else {
                     return 0;
                 }
             }
         });
    }
}
